package com.aizhixin.lab.project.course.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.project.course.domain.DeleteDccumentIdsDomain;
import com.aizhixin.lab.project.course.domain.ProjectDocumentCheckDomain;
import com.aizhixin.lab.project.course.domain.ProjectDocumentDomain;
import com.aizhixin.lab.project.course.domain.QueryProjectDocumentDomain;
import com.aizhixin.lab.project.course.entity.ProjectDocument;
import com.aizhixin.lab.project.course.service.ProjectDocumentService;
import com.aizhixin.lab.project.course.vo.ProjectDocumentVo;
import com.aizhixin.lab.project.course.vo.ProjectMinutesVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RequestMapping("/api/v1/project/document")
@RestController
@Api(value = "项目文档", tags = "项目文档")
public class ProjectDocumentController {
    @Autowired
    private ProjectDocumentService projectDocumentService;
    @Autowired
    private AccountService accountService;
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "保存", httpMethod = "POST", notes = "保存/编辑<br><br><b>lwq</b>")
    public ResponseEntity<?> save(@RequestHeader("Authorization") String token, @RequestBody ProjectDocumentDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            ProjectDocument save = projectDocumentService.save(domain);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,save);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }


    @RequestMapping(value = "/findDocument", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "文档查询", httpMethod = "GET", notes = "文档查询")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response =  ProjectDocumentVo.class),})
    public ResponseEntity<?> findProject(@RequestHeader("Authorization") String token, @RequestParam(name = "projectId")String projectId,
                                         @RequestParam(name = "pageSize")Integer pageSize,@RequestParam(name = "pageNumber")Integer pageNumber) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        QueryProjectDocumentDomain domain = new QueryProjectDocumentDomain();
        domain.setProjectId(projectId);
        domain.setPageNumber(pageNumber);
        domain.setPageSize(pageSize);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            Map<String, Object> projectMinutesVoPageData = projectDocumentService.findProjectDocument(domain);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,projectMinutesVoPageData);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }
        return   new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除", httpMethod = "POST", notes = "删除")
    public ResponseEntity<?> delete(@RequestHeader("Authorization") String token, @RequestBody DeleteDccumentIdsDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            projectDocumentService.delete(domain);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,"删除成功");
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
        }

        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/check", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "文档审核", httpMethod = "POST", notes = "文档审核")
    public ResponseEntity<?> check(@RequestHeader("Authorization") String token, @RequestBody ProjectDocumentCheckDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            ProjectDocument save = projectDocumentService.check(domain);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,save.getId());
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }

        return new ResponseEntity(result, HttpStatus.OK);
    }

}
